package cn.zmit.kuxi.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.contants.Url;
import cn.zmit.kuxi.entity.ExpressEntity;

import com.lidroid.xutils.BitmapUtils;

public class ExpressRecyAdapter extends
		RecyclerView.Adapter<ExpressRecyAdapter.ViewHolder> {

	public List<ExpressEntity> mCellDataList;
	public Context mContext;
	private BitmapUtils bitmapUtils;

	private static final int TYPE_ITEM = 0;
	private static final int TYPE_FOOTER = 1;

	// 构造方法传入集合和上下文
	public ExpressRecyAdapter(List<ExpressEntity> celldatalist, Context context) {
		this.mCellDataList = celldatalist;
		this.mContext = context;
		bitmapUtils = new BitmapUtils(mContext);
	}

	// viewholder
	public static class ViewHolder extends RecyclerView.ViewHolder {
		TextView tv_phase;
		TextView tv_name;
		TextView tv_status;
		TextView tv_time;
		ImageView iv_pic;

		TextView tv_footer;

		public ViewHolder(View view) {
			super(view);
			tv_phase = (TextView) view.findViewById(R.id.tv_phase);
			tv_name = (TextView) view.findViewById(R.id.tv_name);
			tv_status = (TextView) view.findViewById(R.id.tv_status);
			tv_time = (TextView) view.findViewById(R.id.tv_time);
			iv_pic = (ImageView) view.findViewById(R.id.iv_pic);

			tv_footer = (TextView) view.findViewById(R.id.tv_footer);

		}

		public TextView getTvPhase() {
			return tv_phase;
		}

		public TextView getTvName() {
			return tv_name;
		}

		public TextView getTvStatus() {
			return tv_status;
		}

		public TextView getTvTime() {
			return tv_time;
		}

		public ImageView getIvPic() {
			return iv_pic;
		}

		public TextView getFooter() {
			return tv_footer;
		}
	}

	class FooterViewHolder extends ViewHolder {

		public FooterViewHolder(View view) {
			super(view);
		}

	}

	// 集合长度
	@Override
	public int getItemCount() {
		if (mCellDataList != null && mCellDataList.size() > 0) {
			return mCellDataList.size();
		}
		return 0;
	}

	// 绑定视图
	@Override
	public void onBindViewHolder(final ViewHolder viewHolder, final int i) {
		// 需要强转成自己的ViewHolder
		ViewHolder holder = (ViewHolder) viewHolder;
		holder.getTvPhase().setTextColor(Color.parseColor("#FF0000"));
		holder.getTvPhase()
				.setText("第" + mCellDataList.get(i).getPhase() + "期");
		holder.getTvName().setText(mCellDataList.get(i).getName());
		bitmapUtils.display(holder.iv_pic, Url.IMAGE_PICTURES
				+ mCellDataList.get(i).getPic());
		int status = Integer.parseInt(mCellDataList.get(i).getStatus());
		if (status == 0) {
			holder.getTvStatus().setText("发货状态：未发货");
			holder.getTvTime().setText("发货时间：未发货");

		} else if (status == 1) {
			holder.getTvStatus().setText("发货状态：已发货");
			holder.getTvTime()
					.setText("发货时间：" + mCellDataList.get(i).getTime());
		}

		if (mOnItemClickListener != null) {
			viewHolder.itemView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					mOnItemClickListener.onItemClick(viewHolder.itemView, i);
				}
			});
		}
	}

	// 创建viewholder
	@Override
	public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		// if (viewType == TYPE_ITEM) {
		// return new ViewHolder(View.inflate(parent.getContext(),
		// R.layout.item_express, null));
		// } else if (viewType == TYPE_FOOTER) {
		// // View view = LayoutInflater.from(parent.getContext()).inflate(
		// // R.layout.view_footer, null);
		// // view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
		// // LayoutParams.WRAP_CONTENT));
		// // return new FooterViewHolder(view);
		// return new ViewHolder(View.inflate(parent.getContext(),
		// R.layout.view_footer, null));
		// }
		// return null;
		return new ViewHolder(View.inflate(parent.getContext(),
				R.layout.item_express, null));
	}

	// @Override
	// public int getItemViewType(int position) {
	// if (position + 1 == getItemCount()) {
	// return TYPE_FOOTER;
	// } else {
	// return TYPE_ITEM;
	// }
	// }

	// click listener
	public interface OnItemClickListener {
		void onItemClick(View view, int position);
	}

	private OnItemClickListener mOnItemClickListener;

	public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
		this.mOnItemClickListener = mOnItemClickListener;
	}

}
